package com.graduation.mybatis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.graduation.mybatis.bean.Food;
import com.graduation.mybatis.bean.FoodEntries;
import com.graduation.mybatis.service.FoodEntriesService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (FoodEntries)表控制层
 *
 * @author makejava
 * @since 2025-04-07 19:00:53
 */
@RestController
@RequestMapping("foodEntries")
public class FoodEntriesController {
    /**
     * 服务对象
     */
    @Resource
    private FoodEntriesService foodEntriesService;

    /**
     * 分页查询
     *
     * @param foodEntries 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
//    @GetMapping("/page")
//    public IPage<Food> getFoodPage(@RequestParam(defaultValue = "1") int currentPage,
//                                   @RequestParam(defaultValue = "10") int pageSize) {
//        return foodEntriesService.getFoodPage(currentPage, pageSize);
//    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<FoodEntries> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.foodEntriesService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param foodEntries 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<FoodEntries> add(@RequestBody FoodEntries foodEntries) {
        return foodEntriesService.save(foodEntries) ?
                ResponseEntity.ok(foodEntries) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 编辑数据
     *
     * @param foodEntries 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<FoodEntries> edit(FoodEntries foodEntries) {
        return foodEntriesService.updateById(foodEntries) ?
                ResponseEntity.ok(foodEntries) :
                ResponseEntity.internalServerError().build();
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(this.foodEntriesService.removeById(id));
    }
    /**
     * 根据foodId查询FoodEntries
     * @param foodId
     * @return
     */
    @GetMapping("/{foodId}")
    public List<FoodEntries> getFoodEntriesByFoodId(@PathVariable Long foodId) {
        return foodEntriesService.queryByFoodId(foodId);
    }
}

